// mircoApp/syncMain/syncMainPlugin/syncMainPlugin.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    val: "",
    plugName: "消费积分",
    selectObj: {},
    shop_list: [],
    asset_list: [],
    showPicker: false,
    isclick:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: this.data.themeBaseColor,
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
    this.setData({
      val: '',
      isclick:false
    })
    this.getData();
  },
  onConfirm(e) {
    let {
      value
    } = e.detail
    console.log(value)
    this.setData({
      selectObj: value,
      showPicker: false
    })
  },
  closePicker() {
    this.setData({
      showPicker: false
    })
  },
  showPickerTap() {
    this.setData({
      showPicker: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getData(key = '') {
    let urlstr = "";
    if (this.options.plugin == "integral") {
      urlstr = "plugin.sync-main.frontend.modules.integral.controllers.page.index";
    } else if (this.options.plugin == "love") {
      urlstr = "plugin.sync-main.frontend.modules.love.controllers.page.index";
    } else {
      urlstr = "plugin.sync-main.frontend.modules.point.controllers.page.index";
    }
    app._getNetWork({
      url: app.getNetAddresss(urlstr),
      success: resdata => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            asset_list: res.data.asset_list,
            shop_list: res.data.shop_list
          })
          if (res.data.shop_list.length > 0 && key != "update") {
            this.setData({
              selectObj: res.data.shop_list[0]
            })
          }
          console.log(this.options.plugin)
          if (this.options.plugin == "integral") {
            wx.setNavigationBarTitle({
              title: res.data.integral_name + '划转',
            })
            this.setData({
              plugName: res.data.integral_name
            })
          } else if (this.options.plugin == "love") {
            wx.setNavigationBarTitle({
              title: res.data.love_name + '划转',
            })
            this.setData({
              plugName: res.data.love_name
            })
          } else {
            wx.setNavigationBarTitle({
              title: res.data.point_name + '划转',
            })
            this.setData({
              plugName: res.data.point_name
            })
          }
        } else {
          app.tips(res.msg)
        }
      }
    });
  },
  async transfer() {
    if(this.data.isclick) { 
      this.$toast('正在划转请稍后');
      return
    };
    this.data.isclick = true;
    let urlStr = "";
    let json = {
      transfer_type: this.data.selectObj.key,
      change_value: Number(this.data.val)
    };
    if (this.options.plugin == "integral") {
      urlStr = app.getNetAddresss("plugin.sync-main.frontend.modules.integral.controllers.transfer.index");
    } else if (this.options.plugin == "love") {
      urlStr = app.getNetAddresss("plugin.sync-main.frontend.modules.love.controllers.transfer.index");
    } else{
      urlStr = app.getNetAddresss("plugin.sync-main.frontend.modules.point.controllers.transfer.index");
    }
    app._postNetWork({
      url: urlStr,
      data: json,
      success: resdata => {
        var res = resdata.data;
        if (res.result == 1) {
          app.tips("划转成功")
          this.setData({
            val:""
          })
          this.getData("update");
          this.data.isclick = false;
        } else {
          app.tips(res.msg)
          this.data.isclick = false;
        }
      }
    });
  },
})